package com.etrans.web.gps.base.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.etrans.web.gps.business.system.domain.SysUser;
import com.etrans.web.gps.common.Constant;
import com.etrans.web.gps.common.http.JSONUtil;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.StringUtils;
import com.etrans.web.gps.util.grid.FlexiGridUtil;
import com.etrans.web.gps.util.grid.ParamMap;
import com.etrans.web.gps.util.grid.ParamValueObject;

import sun.misc.BASE64Decoder;


/**
 * Created by E-trans
 * User: R.劳
 * Date: 2017/6/7
 * Time: 16:07
 * To change this template use File | Settings | File Templates.
 */
@SuppressWarnings("deprecation")
public class BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    protected HttpServletRequest request;

    /**
     * 组装参数HashMap
     *
     * @return HashMap<String,Object>   resultMap
     */
    protected HashMap<String,Object> getReqParameters() {
        HashMap<String, Object> resultMap = new HashMap<>();
        Enumeration<String> enString = request.getParameterNames();
        String _value;
        String _key;
        while (enString.hasMoreElements()) {
            _key = enString.nextElement();
            _value = request.getParameter(_key);
            resultMap.put(_key, _value);
        }
        return resultMap;
    }

    /**
     * 获取当前会话的session
     *
     * @return HttpSession
     */
    protected HttpSession getSession(){
        return request.getSession();
    }
    
    /**
	 * 输出JSON数据
	 * 
	 * @param response
	 * @param object
	 */
	protected void ajaxResultJson(HttpServletResponse response, Object object) {
		response.setContentType( "text/html; charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			StringBuffer sb = new StringBuffer();
			sb.append(JSONObject.toJSON(object));
			out.print(sb.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 输出JSON数据，启用Gzip压缩
	 * 
	 * @param response
	 * @param object
	 */
	protected void ajaxResultJsonWithGzip(HttpServletRequest request,HttpServletResponse response, Object object) {
		String jsonResult = JSONUtil.toJson(object);
		resultTextWithGzip(request, response, jsonResult);
	}
	/**
	 * 直接输出文本消息
	 * 
	 * @param response
	 * @param object
	 */
	protected void resultText(HttpServletResponse response, String text) {
 
		response.setContentType( "text/html; charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			StringBuffer sb = new StringBuffer();
			sb.append(text);
			out.print(sb.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 直接输出文本消息，启用Gzip压缩
	 * 
	 * @param response
	 * @param object
	 */
	protected void resultTextWithGzip(HttpServletRequest request,HttpServletResponse response, String text) { 
		response.setContentType( "text/html; charset=utf-8");
		try {
			 response.setHeader("Cache-Control","no-cache");
			 String encoding=request.getHeader("Accept-Encoding");
			 if (encoding!=null && encoding.indexOf("gzip")>=0) { 
				response.setHeader("Content-Encoding","gzip");
				OutputStream o=response.getOutputStream();
				GZIPOutputStream gz=new GZIPOutputStream(o);
				gz.write(text.getBytes("UTF-8"));
				gz.finish();
				gz.close();
				o.close();
			}else {
				resultText(response, text);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    
    /**
     * 获取当前登录用户
     */
    protected SysUser getSysUser(){
    	return (SysUser) this.getSession().getAttribute(Constant.LOGIN_SESSION_NAME);
    }
    
    /**
	 * 
	 * @param request
	 * @param response
	 * @param fileName
	 * @param colModelList 表头集合
	 * @param rowsList 
	 * @param imgsrcList 图片
	 */
	public void exportExls(HttpServletRequest request, HttpServletResponse response, String fileName, 
			List<HashMap<String, Object>> colModelList, List<List<Map<String,Object>>> rowsList,
			List<String> imgsrcList,List<String> sheetList) {
		int picWidth=16;
		OutputStream outputStream = null;
		HSSFWorkbook wb = new HSSFWorkbook();
		
		try {
			outputStream = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
//			String filename = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
			response.setHeader("Content-Disposition", "attachment; filename=" +  java.net.URLEncoder.encode(fileName, "UTF-8")+ ".xls"); 
			response.setContentType("application/msexcel");// 定义输出类型
			response.setCharacterEncoding("utf-8");
			//
			//***************生成图片*******************
			if(imgsrcList!=null){
				createPicSheet(wb, outputStream, imgsrcList,fileName,(short)picWidth);
				
			}
			
			if (sheetList != null) {
				for(int i=0;i<colModelList.size();i++){
					HashMap<String, Object> hashMap=colModelList.get(i);
					String[] pre_titleArray = (String[]) hashMap.get("titleArray");
					String[] pre_columnArray = (String[]) hashMap.get("columnArray");
					String[] pre_widthArray = (String[]) hashMap.get("widthArray");
					createDetailSheet(outputStream, wb , sheetList.get(i), pre_titleArray, pre_columnArray,
							 pre_widthArray, rowsList.get(i));
				}
			}else {
				for(int i=0;i<colModelList.size();i++){
					HashMap<String, Object> hashMap=colModelList.get(i);
					String[] pre_titleArray = (String[]) hashMap.get("titleArray");
					String[] pre_columnArray = (String[]) hashMap.get("columnArray");
					String[] pre_widthArray = (String[]) hashMap.get("widthArray");
					createDetailSheet(outputStream, wb , fileName , pre_titleArray, pre_columnArray,
							 pre_widthArray, rowsList.get(i));
				}
			}
			
			wb.write(outputStream);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param fileName
	 * @param colModelList 表头集合
	 * @param rowsList 
	 * @param imgsrcList 图片
	 * @param sheetList 标签页名
	 */
	public void exportExl(HttpServletRequest request, HttpServletResponse response, String fileName, 
			List<HashMap<String, Object>> colModelList, List<List<Map<String,Object>>> rowsList, 
			List<String> imgsrcList) {
			int picWidth=16;
		
		OutputStream outputStream = null;
		HSSFWorkbook wb = new HSSFWorkbook();
		
		try {
			outputStream = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			//String filename = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
			response.setHeader("Content-Disposition", "attachment; filename=" +  java.net.URLEncoder.encode(fileName, "UTF-8")+ ".xls"); 
			response.setContentType("application/msexcel");// 定义输出类型
			response.setCharacterEncoding("utf-8");
			//
			//***************生成图片*******************
			if(imgsrcList!=null){
				createPicSheet(wb, outputStream, imgsrcList,fileName,(short)picWidth);
				
			}
			
			createNewSheet(wb,outputStream,fileName);
			for(int i=0;i<colModelList.size();i++){
				HashMap<String, Object> hashMap=colModelList.get(i);
				String[] pre_titleArray = (String[]) hashMap.get("titleArray");
				String[] pre_columnArray = (String[]) hashMap.get("columnArray");
				String[] pre_widthArray = (String[]) hashMap.get("widthArray");
				createDetailSheet(outputStream, wb , fileName , pre_titleArray, pre_columnArray,
						 pre_widthArray, rowsList.get(i));
			}
			
			wb.write(outputStream);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				}
			}
		}
	}
	
	public void createDetailSheet(OutputStream outputStream,HSSFWorkbook wb,
			String sheetName, String[] titleArray, String[] columnArray,
			String[] widthArray,List<Map<String,Object>> rows, int...args) {
		int picWidth=0;
		int picHeight=0;
		try {
			if (args.length>0){
				picWidth=args[0];
				picHeight=args[1];
			}
			int curRowCnt=0;
			
			HSSFSheet sheet = wb.createSheet(sheetName);//生成工作表
			HSSFCellStyle cellStyle = wb.createCellStyle();//生成单元格样式
			cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置单元格居中
			
			for(int u=0;u<titleArray.length;u++){
				int width = 110;
				String widthStr = widthArray[u];
				if(widthStr != null && !"".equals(widthStr)){
					width = Integer.parseInt(StringUtils.subZeroAndDot(widthStr));
				}
				sheet.setDefaultColumnStyle((short)u, cellStyle);
				sheet.autoSizeColumn((short)0); //调整宽度
				sheet.setColumnWidth(u, (400/11)*width);
			}
			
			HSSFRow row = null;
			//生成标题
			row = sheet.createRow((short) curRowCnt);
			row.setHeight((short)500);
			row.createCell((short) 0).setCellValue(sheetName);
			HSSFCellStyle t0 = wb.createCellStyle(); 
			Font titleFont=wb.createFont();
			titleFont.setFontHeightInPoints((short) 15);
			titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
			t0.setFont(titleFont);
			t0.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			row.getCell(0).setCellStyle(t0);
			CellRangeAddress cra = new CellRangeAddress(0, 0, 0,  titleArray.length-1);//起始行,结束行,起始列,结束列
			sheet.addMergedRegion(cra);
			curRowCnt++;
			
			
			row = sheet.createRow((short) curRowCnt);
			row.setHeight((short)400);
			
			String signature="制表人："+ getSysUser().getLogin_name() + "          制表日期：" + DateUtil.getDateToString(new Date());
			row.createCell((short) 0).setCellValue(signature);
			HSSFCellStyle t = wb.createCellStyle(); 
			Font titleFont1=wb.createFont();
			titleFont1.setFontHeightInPoints((short) 11);
			t.setFont(titleFont1);
			t.setAlignment(HSSFCellStyle.ALIGN_LEFT);
			row.getCell(0).setCellStyle(t);
			CellRangeAddress cra1 = new CellRangeAddress(1, 1, 0,  titleArray.length-1);//起始行,结束行,起始列,结束列
			sheet.addMergedRegion(cra1);
			
			curRowCnt++;
			
			row = sheet.createRow((short) curRowCnt);
			//生成表头
			for (int q = 0; q < titleArray.length; q++) {
				row.createCell((short) q).setCellValue(new HSSFRichTextString(titleArray[q]));
				HSSFCellStyle t00 = wb.createCellStyle(); 
				Font titleFont0=wb.createFont();
				titleFont0.setFontHeightInPoints((short) 10);
				titleFont0.setBoldweight(Font.BOLDWEIGHT_BOLD);
				t00.setFont(titleFont0);
				t00.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				row.getCell((short) q).setCellStyle(t00);
				if (titleArray[q]=="图片"){
					sheet.setColumnWidth((short)q,  (short) (35.7*picWidth));//设置宽度
				}
			}
			curRowCnt++;
			
			HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
			
			//生成数据行
			if(rows != null && rows.size()>0){
				for(int i = 0 ; i < rows.size(); i++){
					Map<String,Object> map = rows.get(i);
					row = sheet.createRow((short) curRowCnt);
					for(int j = 0 ; j<columnArray.length; j++){
						//判断是否插入图片
						if (columnArray[j]=="picture")
						{
							if (map.get(columnArray[j]) == null) continue;
							row.setHeight((short) (15.625*picHeight)); //设置高度
							HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) j,curRowCnt,(short)j,curRowCnt);
							anchor.setAnchorType(2);
							patriarch.createPicture(anchor , wb.addPicture((byte[])map.get(columnArray[j]),HSSFWorkbook.PICTURE_TYPE_JPEG));
							continue;
						}
						String text = map.get(columnArray[j]) == null ? "" : String.valueOf(map.get(columnArray[j]));
						text = StringUtils.subZeroAndDot(text); //去掉小数后面的0 jw.chen
						row.createCell((short) j).setCellValue(text);
					}
					curRowCnt++;
				}
			}
		
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			e.printStackTrace();
			
		}
		
	}
	
	//创建图片sheet
	public void createPicSheet(HSSFWorkbook wb,OutputStream outputStream,List<String> imgsrcList,String fileName,short width) {
		try {
			HSSFSheet sheet = wb.createSheet("分析图");//生成工作表
			HSSFCellStyle cellStyle = wb.createCellStyle();//生成单元格样式
			cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置单元格居中
			
			int curRowCnt=0;
			HSSFRow row = null;
			//生成标题
			row = sheet.createRow((short) curRowCnt);
			row.setHeight((short)500);
			row.createCell((short) 0).setCellValue(fileName);
			HSSFCellStyle t0 = wb.createCellStyle(); 
			Font titleFont=wb.createFont();
			titleFont.setFontHeightInPoints((short) 15);
			titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
			t0.setFont(titleFont);
			t0.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			row.getCell(0).setCellStyle(t0);
			CellRangeAddress cra = new CellRangeAddress(0, 0, 0,  20);//起始行,结束行,起始列,结束列
			sheet.addMergedRegion(cra);
			curRowCnt++;
			
			
			row = sheet.createRow((short) curRowCnt);
			row.setHeight((short)400);
			
			String signature="制表人："+ this.getSysUser().getLogin_name() + "          日期："+ DateUtil.getDateToString(new Date());
			row.createCell((short) 0).setCellValue(signature);
			HSSFCellStyle t = wb.createCellStyle(); 
			Font titleFont1=wb.createFont();
			titleFont1.setFontHeightInPoints((short) 11);
			t.setFont(titleFont1);
			t.setAlignment(HSSFCellStyle.ALIGN_LEFT);
			row.getCell(0).setCellStyle(t);
			CellRangeAddress cra1 = new CellRangeAddress(1, 1, 0,  20);//起始行,结束行,起始列,结束列
			sheet.addMergedRegion(cra1);
			
			curRowCnt++;
			
			
			 
			row = sheet.createRow((short) curRowCnt);
			HSSFCell cells = row.createCell(0);
			cells.setCellType(HSSFCell.CELL_TYPE_BLANK);
			
			for(int i=0;i<imgsrcList.size();i++){
				String imgsrc=imgsrcList.get(i);
				if(imgsrc!=null&&!"undefined".equals(imgsrc)){
					byte[] b=  base64TOpic(imgsrc);
					ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // 将图片写入流中
					ByteArrayInputStream in = new ByteArrayInputStream(b);
					BufferedImage bufferImg = ImageIO.read(in);   
					ImageIO.write(bufferImg, "PNG", outStream); // 利用HSSFPatriarch将图片写入EXCEL
					HSSFPatriarch patri = sheet.createDrawingPatriarch();
					HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,
					        (short) (1+width*i),  3, (short) (width+width*i), 22);
					row.setHeight((short)-1);
					patri.createPicture(anchor, wb.addPicture(
					        outStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
					outStream.close();
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public byte[] base64TOpic(String imgsrc) {
		if (imgsrc == null){
			return null;
		}
		String[] url = imgsrc.split(",");
		String u = url[1];
		byte[] buffer = null;
		try {
			buffer = new BASE64Decoder().decodeBuffer(u);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer;
	}
	
	/**
	 * 删除表头 目的为了去掉操作 位置等表头
	 * @param colModelList
	 * @param index 表头位置  -1表示不删除 
	 * @return
	 */
	public List<HashMap<String, Object>> removeHeadName(List<String> colModelList,int...index){
		List<HashMap<String, Object>> list=new ArrayList<HashMap<String, Object>>();
		for(int i=0;i<colModelList.size();i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			String cm=colModelList.get(i);
			if(cm==null){
				continue;
			}
			HashMap<String, Object> hashMap= FlexiGridUtil.parseJSONToList(cm);
			String[] pre_titleArray = (String[]) hashMap.get("titleArray");
			String[] pre_columnArray = (String[]) hashMap.get("columnArray");
			String[] pre_widthArray = (String[]) hashMap.get("widthArray");
			if(index[i]==-1){
				map.put("titleArray", pre_titleArray);
				map.put("columnArray", pre_columnArray);
				map.put("widthArray", pre_widthArray);
			}else{
				int lg=pre_titleArray.length-1;
				String[] titleArray = new String[lg];
				String[] columnArray = new String[lg];
				String[] widthArray = new String[lg];
				int m=0;
				for(int j=0;j<pre_titleArray.length;j++){
					if(j!=index[i]){
						titleArray[m] = pre_titleArray[j];
						columnArray[m] = pre_columnArray[j];
						widthArray[m] = pre_widthArray[j];
						m++;
					}
				}
				map.put("titleArray", titleArray);
				map.put("columnArray", columnArray);
				map.put("widthArray", widthArray);
			}
			list.add(map);
		}
		return list;
	}
	
	//创建新的一页
	public void createNewSheet(HSSFWorkbook wb,OutputStream outputStream,String fileName){
		
		//totolTitle
		
		HashMap<String,Object> paramMap=getReqParameters();
		String newSheetName=(String)paramMap.get("newSheetName");
		if(StringUtils.isEmpty(newSheetName)){
			return ;
		}
		/*Map<String, Object> newSheetMap= JSON.parseObject(String.valueOf(paramMap.get("NewSheet")));
		Map<String, Object> titleArr=(Map<String, Object>) newSheetMap.get("titleArr");
		Map<String, Object> objArr=(Map<String, Object>) newSheetMap.get("objArr");
		Map<String, Object> sheetNameArr=(Map<String, Object>) newSheetMap.get("objArr");
		*/
		
		
		HSSFSheet sheet = wb.createSheet(newSheetName);//生成工作表
		HSSFCellStyle cellStyle = wb.createCellStyle();//生成单元格样式
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置单元格居中
		
		int curRowCnt=0;
		HSSFRow row = null;
		//生成标题
		row = sheet.createRow((short) curRowCnt);
		row.setHeight((short)500);
		row.createCell((short) 0).setCellValue(fileName);
		HSSFCellStyle t0 = wb.createCellStyle(); 
		Font titleFont=wb.createFont();
		titleFont.setFontHeightInPoints((short) 15);
		titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		t0.setFont(titleFont);
		t0.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		row.getCell(0).setCellStyle(t0);
		CellRangeAddress cra = new CellRangeAddress(0, 0, 0,  20);//起始行,结束行,起始列,结束列
		sheet.addMergedRegion(cra);
		curRowCnt++;
		
		
		row = sheet.createRow((short) curRowCnt);
		row.setHeight((short)400);
		
		String signature="制表人："+ this.getSysUser().getLogin_name() + "          日期："+ DateUtil.getDateToString(new Date());
		row.createCell((short) 0).setCellValue(signature);
		HSSFCellStyle t = wb.createCellStyle(); 
		Font titleFont1=wb.createFont();
		titleFont1.setFontHeightInPoints((short) 11);
		t.setFont(titleFont1);
		t.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		row.getCell(0).setCellStyle(t);
		CellRangeAddress cra1 = new CellRangeAddress(1, 1, 0,  20);//起始行,结束行,起始列,结束列
		sheet.addMergedRegion(cra1);
		
		curRowCnt++;
		
		row = sheet.createRow((short) curRowCnt);
		HSSFCell cells = row.createCell(0);
		cells.setCellType(HSSFCell.CELL_TYPE_BLANK);
		
		String totolTitle=(String)paramMap.get("totolTitle");
		String totolInfo=(String)paramMap.get("totolInfo");
		HSSFRow infoRow=sheet.createRow(curRowCnt+1);
		HSSFCell  cellInfo=null;
		if(!StringUtils.isEmpty(totolTitle)){
			String[]   totolTitleArr=totolTitle.split(",");
			String[] totolInfoArr = null;
			if(!StringUtils.isEmpty(totolInfo)){
				totolInfoArr=totolInfo.split(",");
			}			
			for(int i=0;i<totolTitleArr.length;i++){
				cells= row.createCell(i);
				cells.setCellValue(totolTitleArr[i]);
				
				if(totolInfoArr!=null){
					
					 cellInfo= infoRow.createCell(i);
					cellInfo.setCellValue(totolInfoArr[i]);
				}
				
			}
		}
	}
	/**
	 * 组装导出时候的参数
	 * @param param
	 * @author chenjiongjiong 2015-04-08
	 * @return
	 */
	protected ParamValueObject parseJSONParamExport(ParamMap param, Integer StartPage, Integer EndPage)
	{
		ParamValueObject rtsParamVo = new ParamValueObject();
		rtsParamVo=this.parseJSONParam(param);
		HashMap<String, Object> MapParams=rtsParamVo.getMapParams();
		//获取页数
		Integer PageSize=rtsParamVo.getPageSize();
		//开始值结束值重新赋值
		MapParams.put(Constant.FROMNUMBER,(StartPage-1)*PageSize);
		MapParams.put(Constant.PAGE_SIZE,(EndPage-StartPage+1)*PageSize);
		rtsParamVo.setMapParams(MapParams);
		return rtsParamVo;
	}
	/**
	 *
	 * @param param
	 * @return
	 */
	protected ParamValueObject parseJSONParam(ParamMap param) {
		HashMap<String, Object> mapParams = new HashMap<String, Object>();
		ParamValueObject rtsParamVo = new ParamValueObject();

		Map<String, Object> mapWhereParam =  (Map<String, Object>) param
				.get(Constant.WHERE_PARAM);
		Map<String, Object> mapOrderParam = (Map<String, Object>) param
				.get(Constant.ORDER_PARAM);

		// 查询条件
		if (mapWhereParam != null && mapWhereParam.size() > 0) {
			Iterator<Map.Entry<String, Object>> iterator = mapWhereParam
					.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry<String, Object> entry = iterator.next();
				mapParams.put(entry.getKey(), entry.getValue());
			}
		}
		// 排序
		if (mapOrderParam != null && mapOrderParam.size() > 0) {
			Iterator<Map.Entry<String, Object>> iterator = mapOrderParam
					.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry<String, Object> entry = iterator.next();
				mapParams.put(entry.getKey(), entry.getValue());
			}
		}

		String jtStartIndex = String.valueOf(param.getWhereParam().get(
				Constant.PAGE));
		String pageSizeStr = String.valueOf(param.getWhereParam().get(
				Constant.PAGE_SIZE));

		mapParams.put(
				Constant.FROMNUMBER,
				(Integer.parseInt(jtStartIndex) - 1)
						* Integer.parseInt(pageSizeStr));
		mapParams.put(Constant.PAGE_SIZE, Integer.parseInt(pageSizeStr));

		rtsParamVo.setMapParams(mapParams);
		rtsParamVo.setFromNumber(Integer.parseInt(jtStartIndex));
		rtsParamVo.setPageSize(Integer.parseInt(pageSizeStr));
		return rtsParamVo;
	}
	/**
	 * 数据导入时获取单元格的值
	 * @param cell
	 * @return
	 */
	public String getCellValue(HSSFCell cell) {  
        String result = new String();
        if(cell == null){
        	return "";
        }
        switch (cell.getCellType()) {  
        case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型  
            if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式  
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
                Date date = cell.getDateCellValue();  
                result = sdf.format(date);  
            } else {  
                double value = cell.getNumericCellValue();  
                CellStyle style = cell.getCellStyle();  
                DecimalFormat format = new DecimalFormat();  
                String temp = style.getDataFormatString();  
                // 单元格设置成常规  
                if (temp.equals("General")) {  
                    format.applyPattern("#");  
                }  
                result = format.format(value);  
            }  
            break;  
        case HSSFCell.CELL_TYPE_STRING:// String类型  
            result = cell.getRichStringCellValue().toString();  
            break;  
        case HSSFCell.CELL_TYPE_BLANK:  
            result = "";  
            break;  
        default:  
            result = "";  
            break;  
        }  
        return result;  
    }  
}
